"""

This script runs through a set of user edit vector files and determines the distributions of edit years in them.

This script cannot be parallelized without memory sharing or concurrency control mechanisms and I don't feel like trying 
to figure out how to use pickle so there.

Specify a directory to read input files from.
Specify a file to write output to.

"""

import sys
import os

import wp_datetime as dt

def main():
	inputDir = sys.argv[1]
	outputFile = sys.argv[2]

	inputFileList = os.listdir(inputDir)

	years = {}
	
	for inputFileName in inputFileList:
		
		inputFilePath = inputDir+'/'+inputFileName
		
		if os.path.isfile(inputFilePath):
			processInput(inputFilePath, years)

	writeOutput(outputFile, findDistributions(years))
	
def findDistributions(years):
	dist = {}
	total = 0
	for year in years.keys():
		total += years[year]
	for year in years.keys():
		dist[year] = float(years[year])/float(total)
	return dist

def processInput(inputFilePath, years):
	print "Processing "+inputFilePath
	inputFileHandle = open(inputFilePath, 'r')
	for line in inputFileHandle:
		year = dt.year_fast(line.split('\t')[2])
		if year not in years:
			years[year] = 0
		years[year] += 1
	inputFileHandle.close()

def writeOutput(outputFile, dist):
	outputFileHandle = open(outputFile, 'w')	
	for key in sorted(dist.keys()):
		outputFileHandle.write(key+'\t'+str(dist[key])+'\n')
	outputFileHandle.close()

main()
